package top.anka1.ims;

import top.anka1.ims.ZooManagementSystem.*;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class ZooGUI extends JFrame {
    // Create two zoo instances as required
    private Zoo southernZone;
    private Zoo northernZone;
    private ZooManager zooManager = new ZooManager();
    Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();

    public void windowCenterAlignment(JFrame frame) {
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        int windowx = (int) screensize.getWidth() / 2 - frame.getWidth() / 2;
        int windowy = (int) screensize.getHeight() / 2 - frame.getHeight() / 2;
        frame.setLocation(windowx, windowy);
    }
    public ZooGUI() {
        // Add zoos to the zoo manager
        zooManager.addZoo(new Zoo("Southern-Zone Zoo"));
        zooManager.addZoo(new Zoo("Northern-Zone Zoo"));
        southernZone = zooManager.getZooByName("Southern-Zone Zoo");
        northernZone = zooManager.getZooByName("Northern-Zone Zoo");
        // Add 5 animals to Southern-Zone Zoo
        southernZone.addAnimal(new Animal("Simba", "African Lion", 6));
        southernZone.addAnimal(new Animal("Dumbo", "African Elephant", 12));
        southernZone.addAnimal(new Animal("Luna", "Gray Wolf", 4));
        southernZone.addAnimal(new Animal("Poe", "Raven", 3));
        southernZone.addAnimal(new Animal("Benny", "Grizzly Bear", 8));

        // Add 5 animals to Northern-Zone Zoo
        northernZone.addAnimal(new Animal("Arctic", "Polar Bear", 7));
        northernZone.addAnimal(new Animal("Blizzard", "Snow Leopard", 5));
        northernZone.addAnimal(new Animal("Frost", "Arctic Fox", 3));
        northernZone.addAnimal(new Animal("Penguin", "Emperor Penguin", 4));
        northernZone.addAnimal(new Animal("Aurora", "Caribou", 6));
    }
    public void displayAnimalGUI(Zoo zoo) {
        JFrame frame = new JFrame(zoo.getName());
        JTextArea textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setFont(new Font("Arial", Font.PLAIN, 14));
        textArea.setText(zoo.displayAnimals());
        JScrollPane scrollPane = new JScrollPane(textArea);
        frame.add(scrollPane);
        frame.setSize(400, 200);
        windowCenterAlignment(frame);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }
    public void chooseMoveZoos() {
        JFrame frame = new JFrame("MOVE ANIMAL BETWEEN ZOOS");
        frame.setSize(500, 200);
        windowCenterAlignment(frame);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JLabel title = new JLabel("----- MOVE ANIMAL BETWEEN ZOOS -----");
        setLayout(new BorderLayout());
        title.setFont(new Font("Arial", Font.BOLD, 16));
        title.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        title.setHorizontalAlignment(SwingConstants.CENTER);
        frame.add(title, BorderLayout.NORTH);
        JPanel buttonGridPanel = new JPanel();
        buttonGridPanel.setLayout(new GridLayout(2, 0, 20, 20));
        JButton southernToNorthernButton = new JButton("Move from Southern-Zone to Northern-Zone");
        southernToNorthernButton.addActionListener(e -> {
            moveAnimalGUI(southernZone, northernZone);
        });
        JButton northernToSouthernButton = new JButton("Move from Northern-Zone to Southern-Zone");
        northernToSouthernButton.addActionListener(e -> {
            moveAnimalGUI(northernZone, southernZone);
        });
        buttonGridPanel.add(southernToNorthernButton);
        buttonGridPanel.add(northernToSouthernButton);
        JPanel wrapper = new JPanel(new BorderLayout());
        wrapper.setBorder(BorderFactory.createEmptyBorder(30, 50, 30, 50)); // 给按钮加边距
        wrapper.add(buttonGridPanel, BorderLayout.CENTER);

        frame.add(wrapper, BorderLayout.CENTER);
        frame.setVisible(true);
    }
    public void moveAnimalGUI(Zoo fromZoo, Zoo toZoo) {
        JFrame frame = new JFrame("Move from " + fromZoo.getName() + " to " + toZoo.getName());
        frame.setSize(500, 300);
        windowCenterAlignment(frame);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JTextArea availableAnimals = new JTextArea();
        availableAnimals.setEditable(false);
        availableAnimals.setFont(new Font("Arial", Font.PLAIN, 14));
        availableAnimals.setText("Available Animals in " + fromZoo.getName() + ":\n");
        availableAnimals.append(fromZoo.displayAnimals());
        JScrollPane scrollPane = new JScrollPane(availableAnimals);
        frame.add(scrollPane, BorderLayout.CENTER);
        JPanel inputPanel = new JPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Input"));
        JLabel animalNameLabel = new JLabel("Enter Animal Name to Move:");
        JTextField animalNameField = new JTextField(10);
        JButton confirmButton = new JButton("Confirm");
        confirmButton.addActionListener(e -> {
            String animalName = animalNameField.getText();
            if (fromZoo.findAnimal(animalName) == -1) {
                JOptionPane.showMessageDialog(frame, "Animal not found in " + fromZoo.getName(), "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (zooManager.moveAnimal(fromZoo.getName(), toZoo.getName(), animalName)) {
                JOptionPane.showMessageDialog(frame, "Animal moved successfully!");
                availableAnimals.setText("Available Animals in " + fromZoo.getName() + ":\n");
                availableAnimals.append(fromZoo.displayAnimals());
            } else {
                JOptionPane.showMessageDialog(frame, "Something went wrong when moving animal.", "Error", JOptionPane.ERROR_MESSAGE);
            };
        });
        inputPanel.add(animalNameLabel);
        inputPanel.add(animalNameField);
        inputPanel.add(confirmButton);
        frame.add(inputPanel, BorderLayout.SOUTH);
        frame.setVisible(true);
    }
    public void chooseAddAnimalZoo() {
        JFrame frame = new JFrame("ADD ANIMAL");
        frame.setSize(400, 200);
        windowCenterAlignment(frame);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JLabel title = new JLabel("----- ADD ANIMAL -----");
        setLayout(new BorderLayout());
        title.setFont(new Font("Arial", Font.BOLD, 16));
        title.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        title.setHorizontalAlignment(SwingConstants.CENTER);
        frame.add(title, BorderLayout.NORTH);
        JPanel buttonGridPanel = new JPanel();
        buttonGridPanel.setLayout(new GridLayout(2, 0, 20, 20));
        JButton southernZooButton = new JButton("Add Animal to Southern-Zone Zoo");
        southernZooButton.addActionListener(e -> {
            addAnimalGUI(southernZone);
        });
        JButton northernZooButton = new JButton("Add Animal to Northern-Zone Zoo");
        northernZooButton.addActionListener(e -> {
            addAnimalGUI(northernZone);
        });
        buttonGridPanel.add(southernZooButton);
        buttonGridPanel.add(northernZooButton);
        JPanel wrapper = new JPanel(new BorderLayout());
        wrapper.setBorder(BorderFactory.createEmptyBorder(30, 50, 30, 50)); // 给按钮加边距
        wrapper.add(buttonGridPanel, BorderLayout.CENTER);

        frame.add(wrapper, BorderLayout.CENTER);
        frame.setVisible(true);
    }
    public void addAnimalGUI(Zoo destinationZoo) {
        JFrame frame = new JFrame("Add Animal");
        frame.setSize(600, 200);
        windowCenterAlignment(frame);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JLabel title = new JLabel("----- ADD ANIMAL -----");
        setLayout(new BorderLayout());
        title.setFont(new Font("Arial", Font.BOLD, 16));
        title.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        title.setHorizontalAlignment(SwingConstants.CENTER);
        frame.add(title, BorderLayout.NORTH);
        JPanel inputPanel = new JPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Input"));
        JLabel animalNameLabel = new JLabel("Animal Name:");
        JTextField animalNameField = new JTextField(10);
        JLabel speciesLabel = new JLabel("Species:");
        JTextField speciesField = new JTextField(10);
        JLabel ageLabel = new JLabel("Age:");
        JTextField ageField = new JTextField(10);
        JButton confirmButton = new JButton("Confirm");
        confirmButton.addActionListener(e -> {
            if (animalNameField.getText().isEmpty()) {
                JOptionPane.showMessageDialog(frame, "Animal name cannot be empty!", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            } else if (speciesField.getText().isEmpty()) {
                JOptionPane.showMessageDialog(frame, "Species cannot be empty!", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            } else if (ageField.getText().isEmpty()) {
                JOptionPane.showMessageDialog(frame, "Age cannot be empty!", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            String animalName = animalNameField.getText();
            String species = speciesField.getText();
            int age;
            try {
                age = Integer.parseInt(ageField.getText());
                if (age < 0) {
                    throw new NumberFormatException();
                }
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(frame, "Invalid age! Please enter a positive integer.");
                return;
            }
            Animal animal = new Animal(animalName, species, age);
            JOptionPane.showMessageDialog(frame, destinationZoo.addAnimal(animal));
            //frame.dispose();
        });
        inputPanel.add(animalNameLabel);
        inputPanel.add(animalNameField);
        inputPanel.add(speciesLabel);
        inputPanel.add(speciesField);
        inputPanel.add(ageLabel);
        inputPanel.add(ageField);
        inputPanel.add(confirmButton);
        frame.add(inputPanel, BorderLayout.CENTER);
        frame.setVisible(true);
    }
    public void chooseRemoveAnimalZoo() {
        JFrame frame = new JFrame("REMOVE ANIMAL");
        frame.setSize(400, 200);
        windowCenterAlignment(frame);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JLabel title = new JLabel("----- REMOVE ANIMAL -----");
        setLayout(new BorderLayout());
        title.setFont(new Font("Arial", Font.BOLD, 16));
        title.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        title.setHorizontalAlignment(SwingConstants.CENTER);
        frame.add(title, BorderLayout.NORTH);
        JPanel buttonGridPanel = new JPanel();
        buttonGridPanel.setLayout(new GridLayout(2, 0, 20, 20));
        JButton southernZooButton = new JButton("Remove Animal from Southern-Zone Zoo");
        southernZooButton.addActionListener(e -> {
            removeAnimalGUI(southernZone);
        });
        JButton northernZooButton = new JButton("Remove Animal from Northern-Zone Zoo");
        northernZooButton.addActionListener(e -> {
            removeAnimalGUI(northernZone);
        });
        buttonGridPanel.add(southernZooButton);
        buttonGridPanel.add(northernZooButton);
        JPanel wrapper = new JPanel(new BorderLayout());
        wrapper.setBorder(BorderFactory.createEmptyBorder(30, 50, 30, 50)); // 给按钮加边距
        wrapper.add(buttonGridPanel, BorderLayout.CENTER);

        frame.add(wrapper, BorderLayout.CENTER);
        frame.setVisible(true);
    }
    public void removeAnimalGUI(Zoo sourceZoo) {
        JFrame frame = new JFrame("Remove Animal");
        frame.setSize(400, 200);
        windowCenterAlignment(frame);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JLabel title = new JLabel("----- REMOVE ANIMAL -----");
        setLayout(new BorderLayout());
        title.setFont(new Font("Arial", Font.BOLD, 16));
        title.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        title.setHorizontalAlignment(SwingConstants.CENTER);
        frame.add(title, BorderLayout.NORTH);
        JPanel inputPanel = new JPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Input"));
        JLabel animalNameLabel = new JLabel("Animal Name:");
        JTextField animalNameField = new JTextField(10);
        JButton confirmButton = new JButton("Confirm");
        confirmButton.addActionListener(e -> {
            String animalName = animalNameField.getText();
            int index = sourceZoo.findAnimal(animalName);
            if (index != -1) {
                JOptionPane.showMessageDialog(frame, sourceZoo.deleteAnimal(animalName));
                //frame.dispose();
            } else {
                JOptionPane.showMessageDialog(frame, "Animal not found in " + sourceZoo.getName(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        });
        inputPanel.add(animalNameLabel);
        inputPanel.add(animalNameField);
        inputPanel.add(confirmButton);
        frame.add(inputPanel, BorderLayout.CENTER);
        frame.setVisible(true);
    }
    public void findAnimal() {
        JFrame frame = new JFrame("FIND ANIMAL");
        frame.setSize(400, 200);
        windowCenterAlignment(frame);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JLabel title = new JLabel("----- FIND ANIMAL -----");
        setLayout(new BorderLayout());
        title.setFont(new Font("Arial", Font.BOLD, 16));
        title.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        title.setHorizontalAlignment(SwingConstants.CENTER);
        frame.add(title, BorderLayout.NORTH);
        JPanel inputPanel = new JPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Input"));
        JLabel animalNameLabel = new JLabel("Animal Name:");
        JTextField animalNameField = new JTextField(10);
        JButton confirmButton = new JButton("Confirm");
        confirmButton.addActionListener(e -> {
            String animalName = animalNameField.getText();
            int index = southernZone.findAnimal(animalName);
            if (index != -1) {
                JFrame animalFrame = new JFrame("Animal Found");
                animalFrame.setSize(400, 200);
                animalFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                animalFrame.setLocationRelativeTo(frame);
                animalFrame.setVisible(true);
                JTextArea animalTextArea = new JTextArea();
                animalTextArea.setEditable(false);
                animalTextArea.setFont(new Font("Arial", Font.PLAIN, 14));
                animalTextArea.setText("Animal found in Southern-Zone Zoo:\n" + southernZone.animals[index].display());
                animalFrame.add(animalTextArea);
            } else {
                index = northernZone.findAnimal(animalName);
                if (index != -1) {
                    JFrame animalFrame = new JFrame("Animal Found");
                    animalFrame.setSize(400, 200);
                    animalFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    animalFrame.setLocationRelativeTo(frame);
                    animalFrame.setVisible(true);
                    JTextArea animalTextArea = new JTextArea();
                    animalTextArea.setEditable(false);
                    animalTextArea.setFont(new Font("Arial", Font.PLAIN, 14));
                    animalTextArea.setText("Animal found in Northern-Zone Zoo:\n" + northernZone.animals[index].display());
                    animalFrame.add(animalTextArea);
                } else {
                    JOptionPane.showMessageDialog(frame, "Animal not found in either zoo.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        inputPanel.add(animalNameLabel);
        inputPanel.add(animalNameField);
        inputPanel.add(confirmButton);
        frame.add(inputPanel, BorderLayout.CENTER);
        frame.setVisible(true);
    }
    public void MainGUI() {
        setTitle("Zoo Management System");
        setSize(800, 500);
        int windowx = (int) screensize.getWidth() / 2 - getWidth() / 2;
        int windowy = (int) screensize.getHeight() / 2 - getHeight() / 2;
        setLocation(windowx, windowy);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());

        // 标题
        Font headLineFont = new Font("Arial", Font.BOLD, 24);
        JLabel headLine = new JLabel("===== ZOO MANAGEMENT SYSTEM =====", SwingConstants.CENTER);
        headLine.setFont(headLineFont);
        headLine.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 10));
        add(headLine, BorderLayout.NORTH);

        // 自动布局按钮的面板
        JPanel buttonGridPanel = new JPanel();
        buttonGridPanel.setLayout(new GridLayout(0, 2, 10, 10)); // 自动行数，2列，间距为20px

        // 创建按钮
        JButton displaySouthernZooButton = new JButton("Display Southern-Zone Zoo Animals");
        displaySouthernZooButton.addActionListener(e -> displayAnimalGUI(southernZone));
        buttonGridPanel.add(displaySouthernZooButton);

        JButton displayNorthernZooButton = new JButton("Display Northern-Zone Zoo Animals");
        displayNorthernZooButton.addActionListener(e -> displayAnimalGUI(northernZone));
        buttonGridPanel.add(displayNorthernZooButton);

        JButton moveAnimalButton = new JButton("Move an Animal Between Zoos");
        moveAnimalButton.addActionListener(e -> chooseMoveZoos());
        buttonGridPanel.add(moveAnimalButton);

        JButton addAnimalButton = new JButton("Add an Animal to a Zoo");
        addAnimalButton.addActionListener(e -> chooseAddAnimalZoo());
        buttonGridPanel.add(addAnimalButton);

        JButton removeAnimalButton = new JButton("Remove an Animal from a Zoo");
        removeAnimalButton.addActionListener(e -> chooseRemoveAnimalZoo());
        buttonGridPanel.add(removeAnimalButton);

        JButton findAnimalButton = new JButton("Find an Animal");
        findAnimalButton.addActionListener(e -> findAnimal());
        buttonGridPanel.add(findAnimalButton);

        JButton aboutButton = new JButton("About");
        aboutButton.addActionListener(e -> {
            SwingUtilities.invokeLater(About_Copyright::new);
        });
        buttonGridPanel.add(aboutButton);

        JButton exitButton = new JButton("Exit");
        exitButton.addActionListener(e -> {
            int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to exit?", "Exit Confirmation", JOptionPane.YES_NO_OPTION);
            if (confirm == JOptionPane.YES_OPTION) {
                //System.exit(0);
                dispose(); // 关闭当前窗口
            }
        });
        buttonGridPanel.add(exitButton);

        // 可继续添加更多按钮
        // JButton anotherButton = new JButton("...");
        // buttonGridPanel.add(anotherButton);

        // 包装一个带边距的外层容器
        JPanel wrapper = new JPanel(new BorderLayout());
        wrapper.setBorder(BorderFactory.createEmptyBorder(30, 50, 30, 50)); // 给按钮加边距
        wrapper.add(buttonGridPanel, BorderLayout.CENTER);

        add(wrapper, BorderLayout.CENTER);
        setVisible(true);

    }
    public Zoo getsouthernZoneZoo() {
        return southernZone;
    }
    public Zoo getnorthernZoneZoo() {
        return northernZone;
    }
    public ArrayList<Animal> getAnimalList(String zooName) {
        return zooManager.listAnimals(zooName);
    }
    public ArrayList<Logistics> getLogisticsList() {
        return zooManager.getLogisticsList();
    }
    public ZooManager getZooManager() {
        return zooManager;
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            ZooGUI zooGUI = new ZooGUI();
            zooGUI.MainGUI();
            zooGUI.setVisible(true);
        });
    }
}
